<template>
  <div class="login">
    <div class="left">
      <div class="top">
        <img src="@/assets/img/login_logo.png" alt class="logo" />
        <span class="t1">黑马面面</span>
        <span class="line"></span>
        <span class="t2">用户登录</span>
      </div>
      <el-form :model="form" class="forms" ref="form" :rules="rules">
        <el-form-item prop="phone">
          <el-input
            class="phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
            v-model="form.phone"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            class="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                class="code"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
                v-model="form.code"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img :src="codeURL" class="codeImg" @click='refresh'/>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop='checked'>
          <el-checkbox v-model="form.checked" label class="read">
            我已阅读并同意
            <el-link type="primary" href="#">用户协议</el-link>和
            <el-link type="primary" href="#">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="Btn" @click="submit">登录</el-button>
          <br />
          <el-button type="primary" class="Btn" @click="toRigister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/img/login_banner_ele.png" />
    <register ref='register'></register>
  </div>
</template>

<script>
import register from './register';
import {login} from '@/api/login.js'
import {localSave,localGet} from '@/utils/local.js'
export default {
  components:{
    register,
  },
  data() {
    return {
      codeURL:process.env.VUE_APP_URL+'/captcha?type=login',
      form: {
        phone: "",    //手机号
        password: "", //密码
        code: "",     //验证码
        checked: ""   //多选框状态
      },
      rules: {
        phone: [
          { required: true, message: "请填入手机号", trigger: "blur" },
          {validator:(rule,value,callback)=>{
            let reg =/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
            if ( reg.test(value)){
              callback()
            }else{
              callback('请正确输入手机号码')
            }
          },trigger:'change'}
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "请输入6至12位的密码", trigger: "blur" }
        ],
        code: [
          {required: true,min: 4,max: 4,message: "请输入验证码",trigger: "blur"}
        ],
        checked:[
          {required:true,message:'请勾选协议',trigger:'change'},
          {validator:(rule,value,callback)=>{
            if (value===true){
              callback()
            }else{
              callback('请阅读并勾选协议')
            }
          },trigger:'change'}
        ]
        
      }
    };
  },
  created(){    
    if(localGet()){
      this.$router.push('/layout')
    }
  },
  methods: {
    toRigister() {
      this.$refs.register.show=true
    },
    submit() {
      this.$refs.form.validate(result=>{
        if(result){
          login({
            phone:this.form.phone,
            password:this.form.password,
            code:this.form.code
          }).then(res=>{
            this.$message.success('登录成功')
            localSave(res.data.token)
            this.$router.push('/layout')
          })
        }
      })
    },
    refresh(){
      this.codeURL = process.env.VUE_APP_URL + '/captcha?type=login&rrr=' + Date.now()
    }
  }
};
</script>

<style lang='less'>
.login {
  width: 100%;
  height: 100%;
  background: linear-gradient(
    225deg,
    rgba(20, 148, 250, 1),
    rgba(2, 198, 250, 1)
  );
  display: flex;
  //主轴方向处理
  justify-content: space-around;
  //侧轴方向处理
  align-items: center;
  .left {
    width: 478px;
    height: 550px;
    background: rgba(245, 245, 245, 1);
    padding: 43px;
    .top {
      display: flex;
      align-items: center;
      margin-bottom: 25px;
      .logo {
        width: 22px;
        height: 17px;
      }
      .t1 {
        font-size: 22px;
        font-family: PingFangSC;
        font-weight: 400;
        color: rgba(13, 12, 12, 1);
        margin: 0 14px 0 17px;
      }
      .line {
        width: 1px;
        height: 28px;
        margin-right: 14px;
        background: rgba(199, 199, 199, 1);
      }
      .t2 {
        font-size: 22px;
        font-family: PingFangSC;
        font-weight: 400;
        color: rgba(13, 12, 12, 1);
      }
    }
    .forms {
      .codeImg {
        width: 100%;
      }
      .read {
        text-align: center;
      }
    }
    .Btn {
      width: 100%;
      margin-top: 25px;
    }
  }
  .right {
    width: 633px;
    height: 435px;
  }
}
</style>